import MySQLdb
import logging
log=logging.getLogger('bishop')

def insert_db(site,news,text,complit='waiting'):
    db=MySQLdb.connect(user='root',passwd='19871987',db='logrus_test_db')
    c=db.cursor()
    try:
        c.execute("""INSERT INTO bishop VALUES(id,'%s','%s','%s','%s')""" % (site,news,text,complit))
        log.debug('\tinserting  %s id db end' % news)
        db.commit()
        c.close()
    except:
        log.exception("exception message")
        c.close()
        log.debug('\tsql_insert %s in db fail' % news)

def select_db(site='',data='waiting'):
    if data=='waiting':
        db=MySQLdb.connect(user='root',passwd='19871987',db='logrus_test_db')
        c=db.cursor()
        c.execute("""SELECT id, text FROM bishop WHERE complit='waiting' """)
        sit=c.fetchall()
        spisok=list(sit)
        c.close()
    else:
        db=MySQLdb.connect(user='root',passwd='19871987',db='logrus_test_db')
        c=db.cursor()
        c.execute("""SELECT news FROM bishop WHERE site='%s'""" % site)
        sit=c.fetchall()
        spisok=list(sit)
        c.close()
    return spisok

def update_db(id_):
    db=MySQLdb.connect(user='root',passwd='19871987',db='logrus_test_db')
    c=db.cursor()
    c.execute("""UPDATE bishop SET complit='suc' WHERE id=%s; """ % id_)
    db.commit()
    c.close()

def delit(item,serv):
    """Test function to deleting"""
    db=MySQLdb.connect(user='root',passwd='19871987',db='logrus_test_db')
    c=db.cursor()
    try:
        c.execute("""DELETE FROM bishop WHERE %s='%s'""" % (serv,item))
        db.commit()
        c.close()
        print('OK id - %s' % item)
    except:
        print('FAIL  id - %s' % item)

